3 // HigherOrderMessaging
5 // Created by Ofri Wolfus on 18/05/07.
6 // Copyright 2007 Ofri Wolfus. All rights reserved.
9 #import "DPArrayTests.h"
10 #import "DPCollectionEnumeration.h"
13 @implementation DPArrayTests
15 //=============================================
16 // NOTE: Tests are executed from the bottom up
17 //=============================================
19 - (BOOL)testCollectEach {
20 NSArray *arr1 = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
21 NSArray *arr2 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
22 NSArray *result = [NSArray arrayWithObjects:@"a1", @"b2", @"c3", nil];
24 // -collect looks for "enumerated arguments" (the result of -each) in the passed message.
25 // If found, every iteration on the passed message will include the next object of the
26 // enumerated argument. In our case, every object in arr1 will receive a stringByAppendingString:
27 // message with the matching object in arr2.
28 return [[arr1 collect:MSG(stringByAppendingString:[arr2 each])] isEqualToArray:result];
32 NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"abc", nil];
33 NSArray *result = [NSArray arrayWithObjects:@"A", @"B", @"Abc", nil];
35 return [[arr collect:MSG(capitalizedString)] isEqualToArray:result];
38 - (BOOL)testCollect_20Objects {
39 NSMutableArray *arr = [NSMutableArray array];
40 NSMutableArray *result = [NSMutableArray array];
42 for (i = 0; i < 20; i++) {
43 [arr addObject:[NSString stringWithFormat:@"%d", i]];
44 [result addObject:[NSString stringWithFormat:@"%da", i]];
47 return [[arr collect:MSG(stringByAppendingString:@"a")] isEqualToArray:result];
50 // There's no need to fully test rejectWhere: as it uses the
51 // same implementation of selectWhere:
52 - (BOOL)testRejectWhere {
53 NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"abc", nil];
54 NSArray *result = [NSArray arrayWithObject:@"b"];
56 return [[arr rejectWhere:MSG(hasPrefix:@"a"), nil] isEqualToArray:result];
59 - (BOOL)testSelectWhereEach_twoMessages {
60 NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"c", @"abc", nil];
61 NSArray *suffixes = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
62 NSArray *prefixes = [NSArray arrayWithObjects:@"a", @"b", @"a", @"a", nil];
63 NSArray *result = [NSArray arrayWithObjects:@"a", @"b", @"abc", nil];
65 return [[arr selectWhere:MSG(stringByAppendingString:[suffixes each]),
66 MSG(hasPrefix:[prefixes each]), nil] isEqualToArray:result];
69 - (BOOL)testSelectWhere_twoMessages {
70 NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"abc", nil];
71 NSArray *result = [NSArray arrayWithObjects:@"a", @"abc", nil];
73 return [[arr selectWhere:MSG(capitalizedString), MSG(hasPrefix:@"A"), nil] isEqualToArray:result];
76 - (BOOL)testSelectWhereEach_oneMessage {
77 NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"c", @"abc", nil];
78 NSArray *prefixes = [NSArray arrayWithObjects:@"a", @"b", @"a", @"a", nil];
79 NSArray *result = [NSArray arrayWithObjects:@"a", @"b", @"abc", nil];
81 return [[arr selectWhere:MSG(hasPrefix:[prefixes each]), nil] isEqualToArray:result];
84 - (BOOL)testSelectWhere_oneMessage_30Objects {
85 NSMutableArray *arr = [NSMutableArray array];
86 NSMutableArray *result = [NSMutableArray array];
88 for (i = 0; i < 30; i++) {
89 [arr addObject:[NSString stringWithFormat:@"%d", i]];
91 [result addObject:[NSString stringWithFormat:@"%d", i]];
95 return [[arr selectWhere:MSG(hasSuffix:@"1"), nil] isEqualToArray:result];
98 - (BOOL)testSelectWhere_oneMessage {
99 NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"abc", nil];
100 NSArray *result = [NSArray arrayWithObjects:@"a", @"abc", nil];
102 return [[arr selectWhere:MSG(hasPrefix:@"a"), nil] isEqualToArray:result];
105 - (BOOL)testFindObjectWhereEach_threeMessages {
106 NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"abc", nil];
107 NSArray *suffixes = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
108 NSArray *prefixes = [NSArray arrayWithObjects:@"B", @"A", @"A", nil];
110 return [[arr findObjectWhere:MSG(stringByAppendingString:[suffixes each]),
111 MSG(capitalizedString), MSG(hasPrefix:[prefixes each]),
112 nil] isEqualToString:@"abc"];
115 - (BOOL)testFindObjectWhere_twoMessages {
116 NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"abc", nil];
117 return [[arr findObjectWhere:MSG(capitalizedString), MSG(hasPrefix:@"B"), nil] isEqualToString:@"b"];
120 - (BOOL)testFindObjectWhereEach_oneMessage {
121 NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"abc", nil];
122 NSArray *prefixes = [NSArray arrayWithObjects:@"b", @"a", @"a", nil];
124 return [[arr findObjectWhere:MSG(hasPrefix:[prefixes each]), nil] isEqualToString:@"abc"];
127 - (BOOL)testFindObjectWhere_oneMessage {
128 NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"abc", nil];
129 return [[arr findObjectWhere:MSG(hasPrefix:@"b"), nil] isEqualToString:@"b"];